home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / BetterADsecurity.sit / Better AD security / Source / UninstallCode.c < prev    next >
Text File  |  1996-06-21  |  8KB  |  395 lines

  1. /*    NAME:
  2.         UninstallCode.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         Routines for uninstalling various code resources.
  9.  
  10.     ___________________________________________________________________________
  11. */
  12. //=============================================================================
  13. //        Include files
  14. //-----------------------------------------------------------------------------
  15. #include <Gestalt.h>
  16. #include <Timer.h>
  17. #include "ES.h"
  18. #include "ES Address Table.h"
  19. #include "ExtensionShell.h"
  20. #include "UninstallCode.h"
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. //=============================================================================
  37. //        Private function prototypes
  38. //-----------------------------------------------------------------------------
  39. void    UninstallTrapPatch(short i);
  40. void    UninstallGestaltSelector(short i);
  41. void    UninstallShutdownTask(short i);
  42. void    UninstallVBLTask(short i);
  43. void    UninstallLowMemFilter(short i);
  44. void    UninstallCodeBlock(short i);
  45. void    UninstallTimeManagerTask(short i);
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. //=============================================================================
  62. //        Global variables
  63. //-----------------------------------------------------------------------------
  64. extern ESParamBlock        gTheParamBlock;
  65. extern ESAddressTable    *gTheESAddressTable;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. //=============================================================================
  82. //        UninstallCode : Try and uninstall a code resource from gTheParamBlock.
  83. //-----------------------------------------------------------------------------
  84. void UninstallCode(short i)
  85. {
  86.  
  87.  
  88.     // Case out on the type of the code, and call the uninstall routine
  89.     switch(gTheParamBlock.theCodeResources[i].codeType) {
  90.         case kTrapPatchType:
  91.              UninstallTrapPatch(i);
  92.              break;
  93.              
  94.         case kGestaltSelectorType:
  95.              UninstallGestaltSelector(i);
  96.              break;
  97.              
  98.         case kShutdownTaskType:
  99.              UninstallShutdownTask(i);
  100.              break;
  101.              
  102.         case kVBLTaskType:
  103.              UninstallVBLTask(i);
  104.              break;
  105.         
  106.         case kLowMemFilterType:
  107.              UninstallLowMemFilter(i);
  108.              break;
  109.         
  110.         case kCodeBlockType:
  111.              UninstallCodeBlock(i);
  112.              break;
  113.         
  114.         case kTimeManagerTaskType:
  115.              UninstallTimeManagerTask(i);
  116.              break;
  117.              
  118.         default:
  119.              ;
  120.     }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. //=============================================================================
  137. //        UninstallTrapPatch : Uninstall a trap patch.                                                         
  138. //-----------------------------------------------------------------------------
  139. //        Note :    We check the value currently held in the dispatch table. If
  140. //                it's still the same as our address, then it's safe to remove
  141. //                our routine. We do this just by replacing the dispatch table
  142. //                entry with the old value we originally replaced.
  143. //-----------------------------------------------------------------------------
  144. void UninstallTrapPatch(short i)
  145. {    ProcPtr        currentCode, oldCodeAddress, currentDispatch;
  146.     short        trapWord;
  147.     TrapType    trapType;
  148.     Handle        theHnd;
  149.     
  150.  
  151.     
  152.  
  153.      // Find out what we need to know
  154.      trapWord        = gTheParamBlock.theCodeResources[i].theCodeThing.theTrapPatch.trapWord;
  155.      currentCode        = (ProcPtr) gTheParamBlock.theCodeResources[i].theAddress;
  156.      oldCodeAddress    = gTheESAddressTable->theTable[i];
  157.     trapType        = (trapWord & 0x0800) ? ToolTrap : OSTrap;
  158.     
  159.     
  160.     
  161.     // Compare the value in the dispatch table with our own code. If they match,
  162.     // then it's safe to remove the patch. Otherwise we're stuck.
  163.     currentDispatch = (ProcPtr) StripAddress((Ptr) NGetTrapAddress(trapWord, trapType));
  164.     if (currentDispatch == currentCode)
  165.         {
  166.         // Overwrite the dispatch table entry with the old address.
  167.         NSetTrapAddress((UniversalProcPtr) StripAddress(oldCodeAddress), trapWord, trapType);
  168.    
  169.    
  170.         // Recover a handle to the code we installed, and throw it away
  171.         theHnd = RecoverHandle((Ptr) currentCode);
  172.         DisposeHandle(theHnd);
  173.         }
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. //=============================================================================
  191. //        UninstallGestaltSelector : Uninstall a Gestalt Selector.                                                         
  192. //-----------------------------------------------------------------------------
  193. //        Note :    We can only remove Gestalt Selectors if we've got System 7.5.
  194. //
  195. //                Depending on your Gestalt.h, you may need to comment out
  196. //                the local definition of DeleteGestaltValue.
  197. //-----------------------------------------------------------------------------
  198. extern pascal OSErr DeleteGestaltValue(OSType selector)
  199.  THREEWORDINLINE(0x303C, 0x0203, 0xABF1);
  200. void UninstallGestaltSelector(short i)
  201. {    OSErr                    theErr;
  202.     AGestaltSelector        *theGestaltInfo;
  203.     Handle                    theHnd;
  204.     
  205.  
  206.  
  207.     if (gTheParamBlock.systemVersion >= 0x0750)
  208.         {
  209.         // Remove the selector
  210.         theGestaltInfo    = &gTheParamBlock.theCodeResources[i].theCodeThing.theGestaltSelector;
  211.         theErr            = DeleteGestaltValue(theGestaltInfo->theSelector);
  212.  
  213.  
  214.         // Recover a handle to the code we installed, and throw it away
  215.         theHnd = RecoverHandle(gTheParamBlock.theCodeResources[i].theAddress);
  216.         DisposeHandle(theHnd);
  217.         }
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. //=============================================================================
  235. //        UninstallShutdownTask : Uninstall a Shutdown Task.
  236. //-----------------------------------------------------------------------------
  237. void UninstallShutdownTask(short i)
  238. {    Handle        theHnd;
  239.  
  240.  
  241.     // Remove the shutdown task
  242.     ShutDwnRemove(NewShutDwnProc(gTheParamBlock.theCodeResources[i].theAddress));
  243.  
  244.      
  245.      // Recover a handle to the code and dispose of it
  246.      theHnd = RecoverHandle(gTheParamBlock.theCodeResources[i].theAddress);
  247.     DisposeHandle(theHnd);
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264. //=============================================================================
  265. //        UninstallVBLTask : Uninstall a VBL task.
  266. //-----------------------------------------------------------------------------
  267. void UninstallVBLTask(short i)
  268. {    Handle        theHnd;
  269.  
  270.  
  271.     // Remove the VBL task
  272.     VRemove((QElemPtr) gTheParamBlock.theCodeResources[i].theAddress);
  273.  
  274.      
  275.      // Recover a handle to the code and dispose of it
  276.      theHnd = RecoverHandle(gTheParamBlock.theCodeResources[i].theAddress);
  277.     DisposeHandle(theHnd);
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294. //=============================================================================
  295. //        UninstallLowMemFilter : Remove a low-memory filter.
  296. //-----------------------------------------------------------------------------
  297. void UninstallLowMemFilter(short i)
  298. {    Handle        theHnd;
  299.     ProcPtr        theEntryPoint, currentHead, oldHead, ourCode;
  300.  
  301.  
  302.     
  303.     
  304.     // Get the address of the filter chain
  305.     theEntryPoint = (ProcPtr) gTheParamBlock.theCodeResources[i].theCodeThing.theLowMemFilter.theEntryPoint;
  306.     
  307.     
  308.     
  309.     // Find out where everything else is
  310.     currentHead    = (ProcPtr) *((ProcPtr *) theEntryPoint);
  311.     oldHead        = gTheESAddressTable->theTable[i];
  312.     ourCode        = (ProcPtr) gTheParamBlock.theCodeResources[i].theAddress;
  313.     
  314.     
  315.     
  316.     // Compare the value at the head of the chain with our own code. If
  317.     // they match, then we can remove our filter. Otherwise we're stuck.
  318.     if (currentHead == ourCode)
  319.         {
  320.         // Overwrite the entry at the head of the chain with the original
  321.         *((ProcPtr *) theEntryPoint) = oldHead;
  322.         
  323.         
  324.         
  325.         // Recover a handle to our code, and throw it away
  326.         theHnd = RecoverHandle((Ptr) ourCode);
  327.         DisposeHandle(theHnd);
  328.         }
  329. }
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. //=============================================================================
  346. //        UninstallCodeBlock : Remove a block of code.
  347. //-----------------------------------------------------------------------------
  348. void UninstallCodeBlock(short i)
  349. {    ProcPtr        theCode;
  350.     Handle        theHnd;
  351.     
  352.  
  353.     
  354.  
  355.      // Get the pointer to the code block
  356.      theCode = (ProcPtr) gTheParamBlock.theCodeResources[i].theAddress;
  357.  
  358.      
  359.      // Recover a handle to the code and dispose of it
  360.      theHnd = RecoverHandle((Ptr) theCode);
  361.     DisposeHandle(theHnd);
  362. }
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378. //=============================================================================
  379. //        UninstallTimeManagerTask : Remove a Time Manager task.
  380. //-----------------------------------------------------------------------------
  381. void UninstallTimeManagerTask(short i)
  382. {    Handle        theHnd;
  383.  
  384.  
  385.  
  386.     // Remove the Time Manager task
  387.     RmvTime((QElemPtr) gTheParamBlock.theCodeResources[i].theAddress);
  388.  
  389.      
  390.      // Recover a handle to the code and dispose of it
  391.      theHnd = RecoverHandle(gTheParamBlock.theCodeResources[i].theAddress);
  392.     DisposeHandle(theHnd);
  393. }
  394.